package org.example.designmodel.masterwork;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MasterTest {

    static class SimpleTask extends Task {
        @Override
        public Integer doExecute() {
            System.out.println("task " + getId() + " is done");
            return getId();
        }
    }

    public static void main(String[] args) {
        ScheduledExecutorService service = Executors.newScheduledThreadPool(10);

        Master master = new Master(4);
        service.scheduleAtFixedRate(()->{
            // 1秒钟执行一次
            master.submit(new SimpleTask());
        }, 0, 1, TimeUnit.SECONDS);

        service.scheduleAtFixedRate(()->{
            master.printResult();
        }, 0, 5, TimeUnit.SECONDS);
    }
}
